package com.imut.lagain.repository;
import org.slf4j.LoggerFactory;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imut.lagain.entity.HealthReminder;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 健康提醒数据访问层
 */
@Mapper
public interface HealthReminderRepository extends BaseMapper<HealthReminder> {
    
    /**
     * 根据用户ID和提醒时间范围查询健康提醒
     * @param userId 用户ID
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 健康提醒列表
     */
    @Select("SELECT * FROM health_reminder WHERE user_id = #{userId} AND remind_time BETWEEN #{startTime} AND #{endTime} ORDER BY remind_time ASC")
    List<HealthReminder> findByUserIdAndTimeRange(@Param("userId") Long userId, @Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);
    
    /**
     * 根据用户ID和提醒类型查询健康提醒
     * @param userId 用户ID
     * @param reminderType 提醒类型
     * @param page 分页参数
     * @return 健康提醒分页列表
     */
    @Select("SELECT * FROM health_reminder WHERE user_id = #{userId} AND reminder_type = #{reminderType} ORDER BY remind_time DESC")
    IPage<HealthReminder> findByUserIdAndType(@Param("userId") Long userId, @Param("reminderType") String reminderType, Page<HealthReminder> page);
    
    /**
     * 查询待处理的健康提醒
     * @param userId 用户ID
     * @param currentTime 当前时间
     * @return 健康提醒列表
     */
    @Select("SELECT * FROM health_reminder WHERE user_id = #{userId} AND is_completed = 0 AND remind_time <= #{currentTime} ORDER BY remind_time ASC")
    List<HealthReminder> findPendingReminders(@Param("userId") Long userId, @Param("currentTime") LocalDateTime currentTime);
    
    /**
     * 标记提醒为已完成
     * @param id 提醒ID
     * @return 影响行数
     */
    @Update("UPDATE health_reminder SET is_completed = 1, update_time = NOW() WHERE id = #{id}")
    int markAsCompleted(@Param("id") Long id);
    
    /**
     * 根据用户ID查询未完成的提醒数量
     * @param userId 用户ID
     * @return 未完成的提醒数量
     */
    @Select("SELECT COUNT(*) FROM health_reminder WHERE user_id = #{userId} AND is_completed = 0")
    Long countPendingReminders(@Param("userId") Long userId);
}
